﻿var BaseParameterInputConfiguration = function (jsonOfObject, valueExtensions) {
    var self = this;
    
    self.Id = ko.observable(null);
    self.Feature = ko.observable(null);
    self.Value = ko.observable(null).extend(valueExtensions);

    if (jsonOfObject)
        ko.mapping.fromJS(jsonOfObject, {}, self);

    self.Template = function () {
        return 'ParameterInput'.concat(self.Feature().replace(' ', ''));
    }

    self.OriginalValue = ko.observable(self.Value());

    self.IsDirty = ko.observable(false);

    self.Value.subscribe(function(val) {
        self.IsDirty(self.OriginalValue() != val);
    }, self);

    self.OnUpdateComplete = function() {
        self.OriginalValue(self.Value());
        self.IsDirty(false);
    }
};

var ParameterInputMaximumLengthConfiguration = function (jsonOfObject) {
    var self = this;
    ko.utils.extend(self, new BaseParameterInputConfiguration(jsonOfObject, { pattern: { message: "Numeric input only", params: '^[0-9]+$' } }));
};

var ParameterInputRequiredConfiguration = function (jsonOfObject) {
    var self = this;
    ko.utils.extend(self, new BaseParameterInputConfiguration(jsonOfObject, {}));
    self.IsRequired = ko.observable(self.Value() == 'true');
    self.IsRequired.subscribe(function() {
        self.Value(self.IsRequired() + "");
    }, self);
};
